﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

namespace BtoBSWT315.User
{
    public partial class Edit_cp : System.Web.UI.Page
    {
        public int SortID, TypeID;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!IsPostBack)
            {
                EditDataBind();
            }
        }


        private void EditDataBind()
        {
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            string strSortId = string.Empty;
            string strTypeId = string.Empty;

            //定义所需SQL语句
            string sql = "select * from spzs where id=" + ID;
            OleDbDataReader dr = classdb.Getdr(sql);
            if (dr.Read())
            {
                cpmc.Text = dr["cpmc"].ToString();                     //产品名称
                cpbh.Text = dr["cpbh"].ToString();                     //产品编号
                cpsb.Text = dr["cpsb"].ToString();                     //产品商标
                cpcd.Text = dr["cpcd"].ToString();                     //产品产地
                cpgg.Text = dr["cpgg"].ToString();                     //产品规格
                cpjg.Text = dr["cpjg"].ToString();                     //参考价格
                Picture.Text = dr["Picture"].ToString();               //产品图片
                jysm.Text = dr["jysm"].ToString();                     //简要说明
                Edit_cp_content.Text = dr["xxsm"].ToString();        //产品描述			
                lxr.Text = dr["lxr"].ToString();                       //联系人
                sccj.Text = dr["sccj"].ToString();                     //公司名称
                address.Text = dr["address"].ToString();               //公司地址
                sf.Text = dr["sf"].ToString();                         //所在省份
                city.Text = dr["city"].ToString();                     //所在城市
                phone.Text = dr["phone"].ToString();                   //联系电话
                fax.Text = dr["fax"].ToString();                       //传真
                email.Text = dr["email"].ToString();                   //邮件
                web.Text = dr["web"].ToString();                       //网址
                mobile.Text = dr["mobile"].ToString();               //手机

                strSortId = dr["sortid"].ToString();                     //行业大类
                strTypeId = dr["TypeID"].ToString();                     //行业小类
            }
            dr.Close();

            //定义所需SQL语句
            string sql3 = "select * from class_1";
            //引入数据库操作类
            DataTable dataTable = null;
            dataTable = classdb.GetTables(sql3);
            if (dataTable != null)
            {
                BigClass.DataSource = dataTable.DefaultView;
                BigClass.DataTextField = "Sort";
                BigClass.DataValueField = "SortID";
                BigClass.DataBind();
                BigClass.SelectedValue = strSortId;                    //行业大类
                dataTable = null;
            }

            //定义所需SQL语句
            string sql4 = "select * from class_2 where sortid=" + strSortId;
            dataTable = classdb.GetTables(sql4);
            if (dataTable != null)
            {
                SmallClass.DataSource = dataTable.DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();
                SmallClass.SelectedValue = strTypeId;                    //行业小类
            }
        }



        private void BigClass_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //定义所需SQL语句
            string sql = "select * from class_2 where sortid=" + BigClass.SelectedValue;
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            DataTable dataTable = classdb.GetTables(sql);
            if (dataTable != null)
            {
                SmallClass.DataSource = dataTable.DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();                //行业小类
                dataTable = null;
            }
        }
        private void Button1_Click(object sender, System.EventArgs e)
        {
            if ("" == cpmc.Text.ToString())
            {
                Response.Write("<script>alert('请按要求输入');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }

            //重新换图片（删除旧的，上传新的）
            if (!string.IsNullOrEmpty(DefaultPicUrl.PostedFile.FileName))
            {
                if (Admin.HtmlInputFileControl.IsAllowedExtension(DefaultPicUrl))
                {
                    if (Admin.HtmlInputFileControl.IsAllowedLength(DefaultPicUrl))
                    {
                        Picture.Text = Admin.HtmlInputFileControl.CoverFile(DefaultPicUrl, Picture.Text, "Products");
                    }
                    else
                    {
                        Response.Write("<script>alert('上传图片过大，请选择较小的图片上传！');</script>");
                        Response.Write("<script>history.go(-1);</script>");
                        Response.End();
                    }
                }
                else
                {
                    Response.Write("<script>alert('只支持gif/jpg/jpeg/bmp/png');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
            }

            int ID = Convert.ToInt32(Request.QueryString["ID"]);
            //定义所需SQL
            string sql = "update spzs set Cpmc='" + cpmc.Text + "',cpbh='" + cpbh.Text + "',cpsb='" + cpsb.Text + "',cpcd='" + cpcd.Text + "',cpgg='" + cpgg.Text + "',sortid='" + BigClass.SelectedValue + "',typeid='" + SmallClass.SelectedValue + "'";
            sql += ",cpjg='" + cpjg.Text + "',Picture='" + Picture.Text + "',jysm='" + jysm.Text + "',xxsm='" + Edit_cp_content.Text + "',lxr='" + lxr.Text + "',sccj='" + sccj.Text + "'";
            sql += ",address='" + address.Text + "',sf='" + sf.Text + "',city='" + city.Text + "',phone='" + phone.Text + "',fax='" + fax.Text + "'";
            sql += ",email='" + email.Text + "',web='" + web.Text + "',mobile='" + mobile.Text + "'";
            sql += " where ID=" + ID;
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            if (classdb.GetNonResult(sql) != 0)
            {
                Response.Write("<script>alert('操作成功，请刷新！');</script>");
                Response.Write("<script>history.go(-2);</script>");
                Response.End();
            }
            else
            {
                Response.Write("<script>alert('操作失败！请重新操作！');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }

        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.BigClass.SelectedIndexChanged += new System.EventHandler(this.BigClass_SelectedIndexChanged);
            //this.Upload.Click += new System.EventHandler(this.Upload_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

    }
}